<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li class="active"><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 生成项目">1. 生成项目</a></li><li><a href="#t12.跑通路由和仓库">2.跑通路由和仓库</a><ul><li><a href="#t22.1 history.js">2.1 history.js</a></li><li><a href="#t32.2 src/index.js">2.2 src/index.js</a></li><li><a href="#t42.3 src/containers/App.js">2.3 src/containers/App.js</a></li><li><a href="#t52.4 store/index.js">2.4 store/index.js</a></li><li><a href="#t62.5 store/reducers/index.js">2.5 store/reducers/index.js</a></li><li><a href="#t72.6 store/reducers/user.js">2.6 store/reducers/user.js</a></li></ul></li><li><a href="#t83. 连接组件和仓库">3. 连接组件和仓库</a><ul><li><a href="#t93.1 NavHeader.js">3.1 NavHeader.js</a></li><li><a href="#t103.2 Home.js">3.2 Home.js</a></li><li><a href="#t113.3 Signin.js">3.3 Signin.js</a></li><li><a href="#t123.4 Signup.js">3.4 Signup.js</a></li><li><a href="#t133.5 article/Add.js">3.5 article/Add.js</a></li><li><a href="#t143.6 reducers/user.js">3.6 reducers/user.js</a></li></ul></li><li><a href="#t154. 用户登录">4. 用户登录</a><ul><li><a href="#t164.1 NavHeader.js">4.1 NavHeader.js</a></li><li><a href="#t174.2 App.js">4.2 App.js</a></li><li><a href="#t184.3 Signin.js">4.3 Signin.js</a></li><li><a href="#t194.4 action-types.js">4.4 action-types.js</a></li><li><a href="#t204.5 index.js">4.5 index.js</a></li><li><a href="#t214.6 saga.js">4.6 saga.js</a></li><li><a href="#t224.7 user.js">4.7 user.js</a></li><li><a href="#t234.8 index.js">4.8 index.js</a></li><li><a href="#t244.9 user.js">4.9 user.js</a></li><li><a href="#t254.10 index.js">4.10 index.js</a></li><li><a href="#t264.11 user.js">4.11 user.js</a></li><li><a href="#t274.12 jwt.js">4.12 jwt.js</a></li></ul></li><li><a href="#t285. 发表文章权限控制">5. 发表文章权限控制</a><ul><li><a href="#t295.1 article/Add.js">5.1 article/Add.js</a></li><li><a href="#t305.2 action-types.js">5.2 action-types.js</a></li><li><a href="#t315.3 saga.js">5.3 saga.js</a></li><li><a href="#t325.4 actions/article.js">5.4 actions/article.js</a></li><li><a href="#t335.5 article.js">5.5 article.js</a></li><li><a href="#t345.6 api/index.js">5.6 api/index.js</a></li><li><a href="#t355.7 api/user.js">5.7 api/user.js</a></li><li><a href="#t365.8 reducers/article.js">5.8 reducers/article.js</a></li><li><a href="#t375.9 reducers/index.js">5.9 reducers/index.js</a></li></ul></li><li><a href="#t38参考仓库">参考仓库</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x751F;&#x6210;&#x9879;&#x76EE;">1. &#x751F;&#x6210;&#x9879;&#x76EE; <a href="#t01. &#x751F;&#x6210;&#x9879;&#x76EE;"> # </a></h2>
<pre><code class="lang-js">$ create-react-app jwt-frontend
cd jwt-frontend
cnpm i redux react-redux redux-logger redux-saga react-router-dom react-router-redux@next history axios jsonwebtoken -S
</code></pre>
<h2 id="t12.&#x8DD1;&#x901A;&#x8DEF;&#x7531;&#x548C;&#x4ED3;&#x5E93;">2.&#x8DD1;&#x901A;&#x8DEF;&#x7531;&#x548C;&#x4ED3;&#x5E93; <a href="#t12.&#x8DD1;&#x901A;&#x8DEF;&#x7531;&#x548C;&#x4ED3;&#x5E93;"> # </a></h2>
<h3 id="t22.1 history.js">2.1 history.js <a href="#t22.1 history.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> createHistory <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;history/createBrowserHistory&apos;</span>;
<span class="hljs-keyword">let</span> history=createHistory();
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> history;
</code></pre>
<h3 id="t32.2 src/index.js">2.2 src/index.js <a href="#t32.2 src/index.js"> # </a></h3>
<pre><code class="lang-diff"><span class="hljs-addition">+ import {Provider} from &apos;react-redux&apos;;</span>
<span class="hljs-addition">+ import store from &apos;./store&apos;;</span>
<span class="hljs-addition">+ import App from &apos;./containers/App&apos;;</span>
<span class="hljs-addition">+ import {ConnectedRouter} from &apos;react-router-redux&apos;;</span>
<span class="hljs-addition">+ import history from &apos;./history&apos;;</span>
<span class="hljs-addition">+ import &apos;bootstrap/dist/css/bootstrap.css&apos;</span>
<span class="hljs-addition">+ ReactDOM.render(&lt;Provider store={store}&gt;</span>
<span class="hljs-addition">+     &lt;ConnectedRouter history={history}&gt;</span>
<span class="hljs-addition">+         &lt;App /&gt;</span>
<span class="hljs-addition">+     &lt;/ConnectedRouter&gt;</span>
<span class="hljs-addition">+ &lt;/Provider&gt;, document.getElementById(&apos;root&apos;));</span>
</code></pre>
<h3 id="t42.3 src/containers/App.js">2.3 src/containers/App.js <a href="#t42.3 src/containers/App.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component,Fragment} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">App</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-default&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container-fluid&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;&#x535A;&#x5BA2;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signup&quot;</span>&gt;</span>&#x6CE8;&#x518C;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signin&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/articles/add&quot;</span>&gt;</span>&#x53D1;&#x8868;&#x6587;&#x7AE0;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<h3 id="t52.4 store/index.js">2.4 store/index.js <a href="#t52.4 store/index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {createStore} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> reducers <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./reducers&apos;</span>;
<span class="hljs-keyword">let</span> store=createStore(reducers);
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> store;
</code></pre>
<h3 id="t62.5 store/reducers/index.js">2.5 store/reducers/index.js <a href="#t62.5 store/reducers/index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {combineReducers} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>;
<span class="hljs-keyword">import</span> user <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./user&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> combineReducers({
  user
});
</code></pre>
<h3 id="t72.6 store/reducers/user.js">2.6 store/reducers/user.js <a href="#t72.6 store/reducers/user.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> initState={};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state=initState,action</span>) </span>{
    <span class="hljs-keyword">return</span> state;
}
</code></pre>
<h2 id="t83. &#x8FDE;&#x63A5;&#x7EC4;&#x4EF6;&#x548C;&#x4ED3;&#x5E93;">3. &#x8FDE;&#x63A5;&#x7EC4;&#x4EF6;&#x548C;&#x4ED3;&#x5E93; <a href="#t83. &#x8FDE;&#x63A5;&#x7EC4;&#x4EF6;&#x548C;&#x4ED3;&#x5E93;"> # </a></h2>
<h3 id="t93.1 NavHeader.js">3.1 NavHeader.js <a href="#t93.1 NavHeader.js"> # </a></h3>
<p>src/components/NavHeader.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> {connect} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NavHeader</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar navbar-default&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;container-fluid&quot;</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-brand&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span>&gt;</span>&#x73E0;&#x5CF0;&#x535A;&#x5BA2;<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav&quot;</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/&quot;</span>&gt;</span>&#x9996;&#x9875;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signup&quot;</span>&gt;</span>&#x6CE8;&#x518C;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signin&quot;</span>&gt;</span>&#x767B;&#x5F55;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/articles/add&quot;</span>&gt;</span>&#x53D1;&#x8868;&#x6587;&#x7AE0;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                           <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;nav navbar-nav navbar-right&quot;</span>&gt;</span>
                           <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                               <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signout&quot;</span>&gt;</span>&#x6B22;&#x8FCE;:{this.props.username}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span>
                                <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">&quot;/users/signout&quot;</span>&gt;</span>&#x9000;&#x51FA;<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> connect(<span class="hljs-function"><span class="hljs-params">state</span>=&gt;</span>state.user)(NavHeader);
</code></pre>
<h3 id="t103.2 Home.js">3.2 Home.js <a href="#t103.2 Home.js"> # </a></h3>
<p>src/containers/Home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t113.3 Signin.js">3.3 Signin.js <a href="#t113.3 Signin.js"> # </a></h3>
<p>src/containers/Signin.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Signin</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Signin<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t123.4 Signup.js">3.4 Signup.js <a href="#t123.4 Signup.js"> # </a></h3>
<p>src/containers/Signup.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Signup</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Signup<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t133.5 article/Add.js">3.5 article/Add.js <a href="#t133.5 article/Add.js"> # </a></h3>
<p>src/containers/article/Add.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Add</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Article Add<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t143.6 reducers/user.js">3.6 reducers/user.js <a href="#t143.6 reducers/user.js"> # </a></h3>
<p>src/store/reducers/user.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-let initState={};</span>
<span class="hljs-addition">+let initState={username:&apos;&#x5F20;&#x4E09;&apos;};</span>
</code></pre>
<h2 id="t154. &#x7528;&#x6237;&#x767B;&#x5F55;">4. &#x7528;&#x6237;&#x767B;&#x5F55; <a href="#t154. &#x7528;&#x6237;&#x767B;&#x5F55;"> # </a></h2>
<h3 id="t164.1 NavHeader.js">4.1 NavHeader.js <a href="#t164.1 NavHeader.js"> # </a></h3>
<p>src/components/NavHeader.js</p>
<pre><code class="lang-diff"> import React,{Component} from &apos;react&apos;
 import {Link} from &apos;react-router-dom&apos;;
 import {connect} from &apos;react-redux&apos;;
<span class="hljs-addition">+import actions from &apos;../store/actions/user&apos;;</span>
 class NavHeader extends Component{
<span class="hljs-addition">+    componentDidMount() {</span>
<span class="hljs-addition">+        this.props.loadUser();</span>
<span class="hljs-addition">+    }</span>
     render() {
         return (
             &lt;nav className=&quot;navbar navbar-default&quot;&gt;
                     &lt;div className=&quot;container-fluid&quot;&gt;
                         &lt;div className=&quot;navbar-header&quot;&gt;
<span class="hljs-deletion">-                            &lt;a className=&quot;navbar-brand&quot; href=&quot;#&quot;&gt;&#x73E0;&#x5CF0;&#x535A;&#x5BA2;&lt;/a&gt;</span>
<span class="hljs-addition">+                            &lt;a className=&quot;navbar-brand&quot; href=&quot;/&quot;&gt;&#x73E0;&#x5CF0;&#x535A;&#x5BA2;&lt;/a&gt;</span>
                         &lt;/div&gt;
                         &lt;div&gt;
                             &lt;ul className=&quot;nav navbar-nav&quot;&gt;
<span class="hljs-deletion">-                                &lt;li&gt;&lt;Link to=&quot;/&quot;&gt;&#x9996;&#x9875;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-deletion">-                                &lt;li&gt;&lt;Link to=&quot;/users/signup&quot;&gt;&#x6CE8;&#x518C;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-deletion">-                                &lt;li&gt;&lt;Link to=&quot;/users/signin&quot;&gt;&#x767B;&#x5F55;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-deletion">-                                &lt;li&gt;&lt;Link to=&quot;/articles/add&quot;&gt;&#x53D1;&#x8868;&#x6587;&#x7AE0;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-deletion">-                            &lt;/ul&gt;</span>
<span class="hljs-deletion">-                           &lt;ul className=&quot;nav navbar-nav navbar-right&quot;&gt;</span>
<span class="hljs-deletion">-                           &lt;li&gt;</span>
<span class="hljs-deletion">-                               &lt;Link to=&quot;/users/signout&quot;&gt;&#x6B22;&#x8FCE;:{this.props.username}&lt;/Link&gt;</span>
<span class="hljs-deletion">-                            &lt;/li&gt;</span>
<span class="hljs-addition">+                            &lt;li&gt;&lt;Link to=&quot;/&quot;&gt;&#x9996;&#x9875;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-addition">+                            {</span>
<span class="hljs-addition">+                                !this.props.user&amp;&amp;(&lt;li&gt;&lt;Link to=&quot;/users/signup&quot;&gt;&#x6CE8;&#x518C;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-addition">+                                )</span>
<span class="hljs-addition">+                            }</span>
<span class="hljs-addition">+                            {</span>
<span class="hljs-addition">+                                !this.props.user&amp;&amp;(&lt;li&gt;&lt;Link to=&quot;/users/signin&quot;&gt;&#x767B;&#x5F55;&lt;/Link&gt;&lt;/li&gt;)</span>
<span class="hljs-addition">+                            }</span>
<span class="hljs-addition">+                            {</span>
<span class="hljs-addition">+                                this.props.user&amp;&amp;this.props.user.admin&amp;&amp;&lt;li&gt;&lt;Link to=&quot;/articles/add&quot;&gt;&#x53D1;&#x8868;&#x6587;&#x7AE0;&lt;/Link&gt;&lt;/li&gt;</span>
<span class="hljs-addition">+                            }</span>
<span class="hljs-addition">+                        &lt;/ul&gt;</span>
<span class="hljs-addition">+                        {</span>
<span class="hljs-addition">+                            this.props.user&amp;&amp;&lt;ul className=&quot;nav navbar-nav navbar-right&quot;&gt;</span>
                             &lt;li&gt;
<span class="hljs-deletion">-                                &lt;Link to=&quot;/users/signout&quot;&gt;&#x9000;&#x51FA;&lt;/Link&gt;</span>
<span class="hljs-deletion">-                            &lt;/li&gt;</span>
<span class="hljs-deletion">-                            &lt;/ul&gt;</span>
<span class="hljs-addition">+                                &lt;Link to=&quot;/users/signout&quot;&gt;&#x6B22;&#x8FCE;:{this.props.user.username}&lt;/Link&gt;</span>
<span class="hljs-addition">+                             &lt;/li&gt;</span>
<span class="hljs-addition">+                             &lt;li&gt;</span>
<span class="hljs-addition">+                                    &lt;a onClick={this.props.logout}&gt;&#x9000;&#x51FA;&lt;/a&gt;</span>
<span class="hljs-addition">+                             &lt;/li&gt;</span>
<span class="hljs-addition">+                             &lt;/ul&gt;</span>
<span class="hljs-addition">+                        }</span>
                         &lt;/div&gt;
                     &lt;/div&gt;
                 &lt;/nav&gt;
         )
     }
 }
<span class="hljs-deletion">-export default connect(state=&gt;state.user)(NavHeader);</span>
<span class="hljs-addition">+export default connect(state=&gt;state.user,actions)(NavHeader);</span>
</code></pre>
<h3 id="t174.2 App.js">4.2 App.js <a href="#t174.2 App.js"> # </a></h3>
<p>src/containers/App.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-import {Link,Route} from &apos;react-router-dom&apos;;</span>
<span class="hljs-addition">+import {Route} from &apos;react-router-dom&apos;;</span>
</code></pre>
<h3 id="t184.3 Signin.js">4.3 Signin.js <a href="#t184.3 Signin.js"> # </a></h3>
<p>src/containers/Signin.js</p>
<pre><code class="lang-diff"> import React,{Component} from &apos;react&apos;
<span class="hljs-deletion">-export default class Signin extends Component{</span>
<span class="hljs-addition">+import actions from &apos;../store/actions/user&apos;;</span>
<span class="hljs-addition">+import {connect} from &apos;react-redux&apos;;</span>
<span class="hljs-addition">+class Signin extends Component{</span>
<span class="hljs-addition">+    handleSubmit=(event) =&gt; {</span>
<span class="hljs-addition">+        event.preventDefault();</span>
<span class="hljs-addition">+        let username=this.username.value;</span>
<span class="hljs-addition">+        let password=this.password.value;</span>
<span class="hljs-addition">+        let user={username,password};</span>
<span class="hljs-addition">+        this.props.login(user);</span>
<span class="hljs-addition">+    }</span>
     render() {
         return (
<span class="hljs-deletion">-            &lt;div&gt;Signin&lt;/div&gt;</span>
<span class="hljs-addition">+            &lt;form onSubmit={this.handleSubmit}&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;label htmlFor=&quot;username&quot;&gt;&#x7528;&#x6237;&#x540D;&lt;/label&gt;</span>
<span class="hljs-addition">+                    &lt;input className=&quot;form-control&quot; ref={input=&gt;this.username=input}/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;label htmlFor=&quot;password&quot;&gt;&#x5BC6;&#x7801;&lt;/label&gt;</span>
<span class="hljs-addition">+                    &lt;input className=&quot;form-control&quot; ref={input=&gt;this.password=input}/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;input type=&quot;submit&quot; className=&quot;btn btn-primary&quot;/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                {</span>
<span class="hljs-addition">+                    this.props.error&amp;&amp;(</span>
<span class="hljs-addition">+                        &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                                &lt;div className=&quot;alert alert-danger&quot;&gt;</span>
<span class="hljs-addition">+                                    {this.props.error.toString()}</span>
<span class="hljs-addition">+                                &lt;/div&gt;</span>
<span class="hljs-addition">+                        &lt;/div&gt;</span>
<span class="hljs-addition">+                    )</span>
<span class="hljs-addition">+                }</span>
<span class="hljs-addition">+                </span>
<span class="hljs-addition">+            &lt;/form&gt;</span>
         )
     }
<span class="hljs-deletion">-}</span>
<span class="hljs-addition">+}</span>
<span class="hljs-addition">+export default connect(</span>
<span class="hljs-addition">+    state =&gt; state.user,</span>
<span class="hljs-addition">+    actions</span>
<span class="hljs-addition">+)(Signin);</span>
</code></pre>
<h3 id="t194.4 action-types.js">4.4 action-types.js <a href="#t194.4 action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-diff">export const LOGIN=&apos;LOGIN&apos;;
export const LOGOUT=&apos;LOGOUT&apos;;
export const LOGIN_SUCCESS=&apos;LOGIN_SUCCESS&apos;;
export const LOGIN_ERROR=&apos;LOGIN_ERROR&apos;;
export const LOGIN_OUT_SUCCESS=&apos;LOGIN_OUT_SUCCESS&apos;;
export const LOAD_USER=&apos;LOAD_USER&apos;;
</code></pre>
<h3 id="t204.5 index.js">4.5 index.js <a href="#t204.5 index.js"> # </a></h3>
<p>src/store/index.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-import {createStore} from &apos;redux&apos;;</span>
<span class="hljs-addition">+import {createStore,applyMiddleware} from &apos;redux&apos;;</span>
 import reducers from &apos;./reducers&apos;;
<span class="hljs-deletion">-let store=createStore(reducers);</span>
<span class="hljs-addition">+import createSagaMiddelware from &apos;redux-saga&apos;;</span>
<span class="hljs-addition">+import logger from &apos;redux-logger&apos;;</span>
<span class="hljs-addition">+import rootSaga from &apos;./saga&apos;;</span>
<span class="hljs-addition">+import {routerMiddleware} from &apos;react-router-redux&apos;;</span>
<span class="hljs-addition">+import history from &apos;../history&apos;;</span>
<span class="hljs-addition">+let router=routerMiddleware(history);</span>
<span class="hljs-addition">+let sagaMiddleware=createSagaMiddelware();</span>
<span class="hljs-addition">+let store=createStore(reducers,applyMiddleware(sagaMiddleware,router,logger));</span>
<span class="hljs-addition">+sagaMiddleware.run(rootSaga);</span>
 export default store;
</code></pre>
<h3 id="t214.6 saga.js">4.6 saga.js <a href="#t214.6 saga.js"> # </a></h3>
<p>src/store/saga.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./action-types&apos;</span>;
<span class="hljs-keyword">import</span> {put,call,take,all,takeEvery} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-saga/effects&apos;</span>;
<span class="hljs-keyword">import</span> {push} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-redux&apos;</span>;
<span class="hljs-keyword">import</span> userApi <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./api/user&apos;</span>;
<span class="hljs-keyword">import</span> {decode} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../utils/jwt&apos;</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">login</span>(<span class="hljs-params">action</span>) </span>{
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response=<span class="hljs-keyword">yield</span> call(userApi.login,action.user);
        <span class="hljs-keyword">let</span> {<span class="hljs-attr">data</span>: {<span class="hljs-attr">data</span>: {token}}}=response;
        <span class="hljs-built_in">window</span>.localStorage.setItem(<span class="hljs-string">&apos;token&apos;</span>,token);
        <span class="hljs-keyword">const</span> user=decode(token);
        <span class="hljs-keyword">yield</span> put({<span class="hljs-attr">type</span>: types.LOGIN_SUCCESS,user});
        <span class="hljs-keyword">yield</span> put(push(<span class="hljs-string">&apos;/&apos;</span>));
    } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-keyword">yield</span> put({<span class="hljs-attr">type</span>: types.LOGIN_ERROR,error});
    }
}
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">logout</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">window</span>.localStorage.removeItem(<span class="hljs-string">&apos;token&apos;</span>);
    <span class="hljs-keyword">yield</span> put({<span class="hljs-attr">type</span>: types.LOGIN_OUT_SUCCESS});
    <span class="hljs-keyword">yield</span> put(push(<span class="hljs-string">&apos;/&apos;</span>));
}
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">loginFlow</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span> takeEvery(types.LOGIN,login);
    <span class="hljs-keyword">yield</span> takeEvery(types.LOGOUT,logout);
}
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">loadUser</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> token = <span class="hljs-built_in">window</span>.localStorage.getItem(<span class="hljs-string">&apos;token&apos;</span>);
    <span class="hljs-keyword">if</span> (token) {
        <span class="hljs-keyword">const</span> user=decode(token);
        <span class="hljs-keyword">if</span> (user) {
            <span class="hljs-keyword">yield</span> put({<span class="hljs-attr">type</span>: types.LOGIN_SUCCESS,user});
            <span class="hljs-keyword">yield</span> put(push(<span class="hljs-string">&apos;/&apos;</span>));
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">yield</span> put(push(<span class="hljs-string">&apos;/users/signin&apos;</span>));
        }
    }
}
<span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">watchLoadUser</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span> takeEvery(types.LOAD_USER,loadUser);
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span>* <span class="hljs-title">rootSaga</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">yield</span> all([loginFlow(),watchLoadUser()]);
}
</code></pre>
<h3 id="t224.7 user.js">4.7 user.js <a href="#t224.7 user.js"> # </a></h3>
<p>src/store/actions/user.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    login(user) {
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.LOGIN,user}
    },
    logout() {
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.LOGOUT};
    },
    loadUser() {
        <span class="hljs-keyword">return</span> {<span class="hljs-attr">type</span>:types.LOAD_USER};
    }
}
</code></pre>
<h3 id="t234.8 index.js">4.8 index.js <a href="#t234.8 index.js"> # </a></h3>
<p>src/store/api/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;axios&apos;</span>;
<span class="hljs-keyword">const</span> BASE_URL=<span class="hljs-string">&apos;http://localhost:8080&apos;</span>;
axios.interceptors.request.use(<span class="hljs-function"><span class="hljs-params">config</span> =&gt;</span> {
    <span class="hljs-keyword">let</span> token=localStorage.getItem(<span class="hljs-string">&apos;token&apos;</span>);
    <span class="hljs-keyword">if</span> (token) {
        config.headers.Authorization=token;
    }
    <span class="hljs-keyword">return</span> config;
});
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">post</span>(<span class="hljs-params">url,body</span>) </span>{
    <span class="hljs-keyword">return</span> axios.post(BASE_URL+<span class="hljs-string">&apos;/users/signin&apos;</span>,body);
}
</code></pre>
<h3 id="t244.9 user.js">4.9 user.js <a href="#t244.9 user.js"> # </a></h3>
<p>src/store/api/user.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {post} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./index&apos;</span>;
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">login</span>(<span class="hljs-params">body</span>) </span>{
    <span class="hljs-keyword">return</span> post(<span class="hljs-string">&apos;/users/signin&apos;</span>,body);    
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    login
}
</code></pre>
<h3 id="t254.10 index.js">4.10 index.js <a href="#t254.10 index.js"> # </a></h3>
<p>src/store/reducers/index.js</p>
<pre><code class="lang-diff"> import {combineReducers} from &apos;redux&apos;;
 import user from &apos;./user&apos;;
<span class="hljs-addition">+import {routerReducer} from &apos;react-router-redux&apos;;</span>
 export default combineReducers({
<span class="hljs-addition">+  router:routerReducer</span>
 });
</code></pre>
<h3 id="t264.11 user.js">4.11 user.js <a href="#t264.11 user.js"> # </a></h3>
<p>src/store/reducers/user.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-let initState={username:&apos;&#x5F20;&#x4E09;&apos;};</span>
<span class="hljs-addition">+import * as types from &apos;../action-types&apos;;</span>
<span class="hljs-addition">+let initState={};</span>
 export default function (state=initState,action) {
<span class="hljs-deletion">-    return state;</span>
<span class="hljs-addition">+    switch (action.type) {</span>
<span class="hljs-addition">+        case types.LOGIN_SUCCESS:</span>
<span class="hljs-addition">+            return {...state,user:action.user,error: null};</span>
<span class="hljs-addition">+        case types.LOGIN_ERROR:</span>
<span class="hljs-addition">+            return {...state,user: null,error: action.error};</span>
<span class="hljs-addition">+            case types.LOGIN_OUT_SUCCESS:</span>
<span class="hljs-addition">+            return {...state,user: null,error: null};</span>
<span class="hljs-addition">+        default:</span>
<span class="hljs-addition">+            return state;</span>
<span class="hljs-addition">+    }</span>
}
</code></pre>
<h3 id="t274.12 jwt.js">4.12 jwt.js <a href="#t274.12 jwt.js"> # </a></h3>
<p>src/utils/jwt.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> jwt <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;jsonwebtoken&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">decode</span>(<span class="hljs-params">token</span>) </span>{
    <span class="hljs-keyword">return</span> jwt.decode(token);;
}
</code></pre>
<h2 id="t285. &#x53D1;&#x8868;&#x6587;&#x7AE0;&#x6743;&#x9650;&#x63A7;&#x5236;">5. &#x53D1;&#x8868;&#x6587;&#x7AE0;&#x6743;&#x9650;&#x63A7;&#x5236; <a href="#t285. &#x53D1;&#x8868;&#x6587;&#x7AE0;&#x6743;&#x9650;&#x63A7;&#x5236;"> # </a></h2>
<h3 id="t295.1 article/Add.js">5.1 article/Add.js <a href="#t295.1 article/Add.js"> # </a></h3>
<p>src/containers/article/Add.js</p>
<pre><code class="lang-diff"> import React,{Component} from &apos;react&apos;
<span class="hljs-deletion">-export default class Add extends Component{</span>
<span class="hljs-addition">+import actions from &apos;../../store/actions/article&apos;;</span>
<span class="hljs-addition">+import {connect} from &apos;react-redux&apos;;</span>
<span class="hljs-addition">+class Signin extends Component{</span>
<span class="hljs-addition">+    handleSubmit=(event) =&gt; {</span>
<span class="hljs-addition">+        event.preventDefault();</span>
<span class="hljs-addition">+        let title=this.title.value;</span>
<span class="hljs-addition">+        let content=this.content.value;</span>
<span class="hljs-addition">+        let article={title,content};</span>
<span class="hljs-addition">+        this.props.add(article);</span>
<span class="hljs-addition">+    }</span>
     render() {
         return (
<span class="hljs-deletion">-            &lt;div&gt;Article Add&lt;/div&gt;</span>
<span class="hljs-addition">+            &lt;form onSubmit={this.handleSubmit}&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;label htmlFor=&quot;title&quot;&gt;&#x6807;&#x9898;&lt;/label&gt;</span>
<span class="hljs-addition">+                    &lt;input className=&quot;form-control&quot; ref={input=&gt;this.title=input}/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;label htmlFor=&quot;content&quot;&gt;&#x5185;&#x5BB9;&lt;/label&gt;</span>
<span class="hljs-addition">+                    &lt;textarea className=&quot;form-control&quot; ref={input=&gt;this.content=input}/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                    &lt;input type=&quot;submit&quot; className=&quot;btn btn-primary&quot;/&gt;</span>
<span class="hljs-addition">+                &lt;/div&gt;</span>
<span class="hljs-addition">+                {</span>
<span class="hljs-addition">+                    this.props.error&amp;&amp;(</span>
<span class="hljs-addition">+                        &lt;div className=&quot;form-group&quot;&gt;</span>
<span class="hljs-addition">+                                &lt;div className=&quot;alert alert-danger&quot;&gt;</span>
<span class="hljs-addition">+                                    {this.props.error.toString()}</span>
<span class="hljs-addition">+                                &lt;/div&gt;</span>
<span class="hljs-addition">+                        &lt;/div&gt;</span>
<span class="hljs-addition">+                    )</span>
<span class="hljs-addition">+                }</span>
<span class="hljs-addition">+                </span>
<span class="hljs-addition">+            &lt;/form&gt;</span>
         )
     }
<span class="hljs-deletion">-}</span>
<span class="hljs-addition">+}</span>
<span class="hljs-addition">+export default connect(</span>
<span class="hljs-addition">+    state =&gt; state.article,</span>
<span class="hljs-addition">+    actions</span>
<span class="hljs-addition">+)(Signin);</span>
</code></pre>
<h3 id="t305.2 action-types.js">5.2 action-types.js <a href="#t305.2 action-types.js"> # </a></h3>
<p>src/store/action-types.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-export const LOAD_USER=&apos;LOAD_USER&apos;;</span>
<span class="hljs-addition">+export const LOAD_USER=&apos;LOAD_USER&apos;;</span>
<span class="hljs-addition">+export const ADD_ARTICLE=&apos;ADD_ARTICLE&apos;;</span>
<span class="hljs-addition">+export const ADD_ARTICLE_SUCCESS=&apos;ADD_ARTICLE_SUCCESS&apos;;</span>
<span class="hljs-addition">+export const ADD_ARTICLE_ERROR=&apos;ADD_ARTICLE_ERROR&apos;;</span>
</code></pre>
<h3 id="t315.3 saga.js">5.3 saga.js <a href="#t315.3 saga.js"> # </a></h3>
<p>src/store/saga.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+function* addArticle(action) {</span>
<span class="hljs-addition">+    try {</span>
<span class="hljs-addition">+        debugger;</span>
<span class="hljs-addition">+        yield call(articleApi.add,action.article);</span>
<span class="hljs-addition">+        yield put(push(&apos;/&apos;));</span>
<span class="hljs-addition">+    } catch (error) {</span>
<span class="hljs-addition">+        yield put({type: types.ADD_ARTICLE_ERROR,error});</span>
<span class="hljs-addition">+    }</span>
<span class="hljs-addition">+}</span>
<span class="hljs-addition">+</span>
<span class="hljs-addition">+function* watchAddArticle() {</span>
<span class="hljs-addition">+    yield takeEvery(types.ADD_ARTICLE,addArticle);</span>
<span class="hljs-addition">+}</span>
<span class="hljs-addition">+</span>
 export default function* rootSaga() {
<span class="hljs-deletion">-    yield all([loginFlow(),watchLoadUser()]);</span>
<span class="hljs-addition">+    yield all([loginFlow(),watchLoadUser(),watchAddArticle()]);</span>
 }
</code></pre>
<h3 id="t325.4 actions/article.js">5.4 actions/article.js <a href="#t325.4 actions/article.js"> # </a></h3>
<p>src/store/actions/article.js</p>
<pre><code class="lang-diff">import * as types from &apos;../action-types&apos;;
export default {
    add(article) {
        return {type:types.ADD_ARTICLE,article}
    }
}
`
</code></pre>
<h3 id="t335.5 article.js">5.5 article.js <a href="#t335.5 article.js"> # </a></h3>
<p>src/store/api/article.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {post} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;.&apos;</span>;
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">body</span>) </span>{
    <span class="hljs-keyword">return</span> post(<span class="hljs-string">&apos;/articles/add&apos;</span>,body);    
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
    add
}
</code></pre>
<h3 id="t345.6 api/index.js">5.6 api/index.js <a href="#t345.6 api/index.js"> # </a></h3>
<p>src/store/api/index.js</p>
<pre><code class="lang-diff"><span class="hljs-deletion">-    return axios.post(BASE_URL+&apos;/users/signin&apos;,body);</span>
<span class="hljs-addition">+    return axios.post(BASE_URL+url,body);</span>
</code></pre>
<h3 id="t355.7 api/user.js">5.7 api/user.js <a href="#t355.7 api/user.js"> # </a></h3>
<p>src/store/api/user.js</p>
<pre><code class="lang-js">-<span class="hljs-keyword">import</span> {post} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./index&apos;</span>;
+<span class="hljs-keyword">import</span> {post} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;.&apos;</span>;
 <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">login</span>(<span class="hljs-params">body</span>) </span>{
     <span class="hljs-keyword">return</span> post(<span class="hljs-string">&apos;/users/signin&apos;</span>,body);    
 }
</code></pre>
<h3 id="t365.8 reducers/article.js">5.8 reducers/article.js <a href="#t365.8 reducers/article.js"> # </a></h3>
<p>src/store/reducers/article.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../action-types&apos;</span>;
<span class="hljs-keyword">let</span> initState={};
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state=initState,action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> types.ADD_ARTICLE_ERROR:
            <span class="hljs-keyword">return</span> {...state,<span class="hljs-attr">user</span>: <span class="hljs-literal">null</span>,<span class="hljs-attr">error</span>: action.error};
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state;
    }
}
</code></pre>
<h3 id="t375.9 reducers/index.js">5.9 reducers/index.js <a href="#t375.9 reducers/index.js"> # </a></h3>
<p>src/store/reducers/index.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+ import {routerReducer} from &apos;react-router-redux&apos;;</span>
export default combineReducers({
  user,
<span class="hljs-addition">+  article,</span>
  router:routerReducer
});
</code></pre>
<h2 id="t38&#x53C2;&#x8003;&#x4ED3;&#x5E93;">&#x53C2;&#x8003;&#x4ED3;&#x5E93; <a href="#t38&#x53C2;&#x8003;&#x4ED3;&#x5E93;"> # </a></h2>
<ul>
<li><a href="https://github.com/ReactTraining/react-router/tree/master/packages/react-router-redux">react-router-redux</a></li>
<li><a href="https://gitee.com/zhufengpeixun/jwt-frontend">jwt-frontend</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
